package com.xxadmin.usercenter.config;

import com.xxadmin.common.constant.ResponseCommonEnum;
import com.xxadmin.common.constant.ResponseEnum;
import com.xxadmin.common.exception.BusinessException;
import com.xxadmin.common.vo.ResponseModel;
import com.xxadmin.common.vo.ResponseModelBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * 全局异常统一处理
 *
 * @author:chenming
 * @date:2018/8/30
 */
@RestControllerAdvice
@Slf4j
public class GlobExceptionAdvice {

    @ExceptionHandler(RuntimeException.class)
    public ResponseModel exceptionHandler(Exception ex){
        ResponseModel responseModel = ResponseModelBuilder.genericErrorResponseModal();

        if(ex instanceof BusinessException){
            BusinessException bex = (BusinessException) ex;
            responseModel.setCode(bex.getErrorCode());
            responseModel.setMessage(bex.getErrorMessage());
        }else if(ex instanceof Exception){
            responseModel.setCode("0001000");
            responseModel.setMessage("系统异常!");
        }

        LOGGER.error(ex.getMessage(),ex);

        return responseModel;
    }

    @ExceptionHandler(value = AuthenticationException.class)
    public Object AuthenticationExceptionHandler(AuthenticationException e) {
        System.out.println("1111111111111111");

        return null;
    }

    /**
     * 用于拦截 Spring mvc  参数校验
     * @param ex
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseModel handleBindException1(MethodArgumentNotValidException ex) {
        BindingResult result = ex.getBindingResult();

        return paramllageExceptionHandler(result);
    }

    /**
     * 用于拦截 Spring mvc  参数校验
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public ResponseModel handleBindException(BindException e) {
        BindingResult result = e.getBindingResult();

        return paramllageExceptionHandler(result);
    }

    /**
     * 处理异常信息
     *
     * @param result
     * @return
     */
    private ResponseModel paramllageExceptionHandler(BindingResult result) {
        ResponseModel responseModel = ResponseModelBuilder.genericResponseModal();
        responseModel.setCode(ResponseCommonEnum.PARAM_ILLAGE.getCode());
        responseModel.setMessage("");

        List<ObjectError> errors = result.getAllErrors();
        for (ObjectError error : errors){
            responseModel.setMessage(responseModel.getMessage() + error.getDefaultMessage() + ",");
        }

        // 去掉最后一个 ,
        responseModel.setMessage(responseModel.getMessage().substring(0,responseModel.getMessage().length() - 1));

        return responseModel;
    }

}
